package com.atguigu.spzx.manager.service.impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.atguigu.spzx.manager.service.ValidateCodeService;
import com.atguigu.spzx.model.vo.system.ValidateCodeVo;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class ValidateCodeServiceImpl implements ValidateCodeService {
    @Resource
    StringRedisTemplate stringRedisTemplate;
    @Override
    public ValidateCodeVo genValidateCode() {
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(140, 40, 4, 8);
        String code = captcha.getCode();
        String imageBase64 = captcha.getImageBase64();
        //保存code值到redis中: 设置过期时间10分钟
        String codeKey = UUID.randomUUID().toString().replace("-", "");
        stringRedisTemplate.opsForValue().set("user:login:code:"+codeKey,code,10, TimeUnit.MINUTES);
        ValidateCodeVo codeVo = new ValidateCodeVo();
        codeVo.setCodeKey(codeKey);
        codeVo.setCodeImg(imageBase64);
        return codeVo;
    }
}